package edu.bit.water.service;

import edu.bit.water.dao.WaterSavingDao;
import edu.bit.water.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标签表的业务层
 * @author HJS
 *
 */
@Service
public class WaterSavingService {

    @Autowired
    private WaterSavingDao waterSavingDao;


    /**
     * 查询WATER_SAVING中的记录
     * @return WATER_SAVING中的相应记录
     * @param label_eva_basis
     */
    public List<WaterSaving> select(String label_eva_basis){
        List<WaterSaving> list = waterSavingDao.select(label_eva_basis);
        return list;
    }

    public List<MetaManagementIndicator> getManagementIndicatorData(String standardNum) {
        List<MetaManagementIndicator> list = waterSavingDao.getManagementIndicatorData(standardNum);
        return list;
    }

    /**
     * 插入记录
     * @param managementIndicator 标签记录实体
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> insertManagementIndicatorData(ManagementIndicator managementIndicator) {
        Map<String, String> modelMap = new HashMap<>();
        try {
            int n = waterSavingDao.insertManagementIndicatorData(managementIndicator);
            if (n == 1)
                modelMap.put("status", "success");
            else
                modelMap.put("status", "failed");
            return modelMap;
        } catch (DuplicateKeyException e) {
            // TODO: handle exception
            if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "创建时间已存在，请重新输入");
                return modelMap;
            }
        }
        return modelMap;
    }

    public List<MetaTI> getTechnicalIndicatorData(String standardNum) {
        List<MetaTI> list = waterSavingDao.getTechnicalIndicatorData(standardNum);
        return list;
    }

    /**
     * 插入记录
     * @param technicalIndicator
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> insertTechnicalIndicatorData(TechnicalIndicator technicalIndicator) {
        Map<String, String> modelMap = new HashMap<>();
        try {
            int n = waterSavingDao.insertTechnicalIndicatorData(technicalIndicator);
            if (n == 1)
                modelMap.put("status", "success");
            else
                modelMap.put("status", "failed");
            return modelMap;
        } catch (DuplicateKeyException e) {
            // TODO: handle exception
            if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "创建时间已存在，请重新输入");
                return modelMap;
            }
        }
        return modelMap;
    }
}
